import java.util.List;
import java.awt.image.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import static java.awt.GraphicsDevice.WindowTranslucency.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class Client extends JFrame implements ActionListener{
    private File fileName;
    private BufferedReader in;
    private PrintWriter out;
	private final static boolean shouldFill = true;
	private ImageButton btnAddFile;
	private ImageButton btnBack;
	private ImageButton sendAFile;
	private ImageButton receiveAFile;
	private ImageButton btnBackShare;
	private ImageButton okayReceive;
	private ImageButton backReceive;
	private Dimension imageBackgroundSize, imageBackgroundSize2,imageBackgroundSize3, screenSize;
	private JFileChooser fileChooser = null;
	protected JFrame mainFrame = new JFrame();
	private JFrame mainFrame2 = new JFrame();
	private JFrame mainFrame3 = new JFrame();
	private GridBagConstraints gc = new GridBagConstraints();
	private GridBagConstraints gc2 = new GridBagConstraints();
	private GridBagConstraints gc3 = new GridBagConstraints();
	private Icon btnAddFile1a, btnBack1a, sendAFile1a, receiveAFile1a, btnBackShare1a, backReceive1a, okayReceive1a;
	private ImageIcon imageBackground, imageBackground2,imageBackground3, btnAddFilePressed, btnBackPressed, sendAFilePressed, receiveAFilePressed, btnBackSharePressed;
	private ImageIcon backReceivePressed, okayReceivePressed;
	private JLabel uploadFileLabel;
	private JLabel inputMessageLabel;
	private JPanel topPanel;
	private JPanel chatPanel;
    private JPanel inputMessagePanel;
    private JPanel fileSharingPanel;
    private JPanel inputFilePanel;
    private JPanel mainPanel;
    private JPanel receivePanel, receivePanel2;
    private JPanel sharingPanel;
    private Socket socket;
    private JTextField inputMessageField, senderIP;
    private JTextArea messageArea;
    private JTextField uploadFileTxtField;
    private JTextField uploadFileTextField;
    
    private JFrame mainFrame4 = new JFrame();
    private Dimension mainFSize;
    private ImageIcon img1;
    private ImageIcon clientButtonPressed,exitButtonPressed;
    private Icon clientButton1a,exitButton1a;
    private JPanel mainPaneTop,mainPaneBottom;
    private ImageButton clientButton,exitButton;
    
	String filename = null;

    public Client(){
    	imageBackground = new ImageIcon("bg.png");
    	imageBackgroundSize = new Dimension(imageBackground.getIconWidth(),imageBackground.getIconHeight());
    	mainFrame.setPreferredSize(imageBackgroundSize);

		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int x = ((int)screenSize.getWidth()/2)-200;
		int y = ((int)screenSize.getHeight()/2)-200;
		setLocation(x,y);

		topPanel = new JPanel(){
            public void paintComponent(Graphics g){
                Image img = new ImageIcon("bg.png").getImage();
                Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
                setPreferredSize(size);
                setMinimumSize(size);
                setMaximumSize(size);
                setSize(size);
                setLayout(null);
                g.drawImage(img, 0, 0, null);
            }
        };
        topPanel.setBounds(0, 0, imageBackground.getIconWidth(), imageBackground.getIconHeight());
				
    	JPanel chattingPanel2 = new JPanel();
    	chattingPanel2.setOpaque(false);
    	
    	inputMessagePanel = new JPanel(new GridBagLayout());
    	inputMessagePanel.setPreferredSize(new Dimension(400,200));
    	inputMessagePanel.setOpaque(false);

    	inputMessageField = new JTextField(35);
    	inputMessageField.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                out.println(inputMessageField.getText());
                inputMessageField.setText("");
            }
        });;

    	gc.gridx = 0;
    	gc.gridy = 0;
    	inputMessagePanel.add(inputMessageField,gc);
    	chattingPanel2.add(inputMessagePanel);
    	
    	JPanel chattingPanel3 = new JPanel();
    	chattingPanel3.setOpaque(false);
		chattingPanel3.setOpaque(false);
		chattingPanel3.setSize(imageBackgroundSize);
		
    	chatPanel = new JPanel();
    	chatPanel.setOpaque(false);
    	chatPanel.setLayout(new GridBagLayout());

    	messageArea = new JTextArea(7, 35);
    	messageArea.setEditable(false);
    	gc.gridx = 0;
    	gc.gridy = 0;
    	gc.fill = GridBagConstraints.HORIZONTAL;
    	JScrollPane scroller = new JScrollPane(messageArea,
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    	chatPanel.add(scroller,gc);
    	chattingPanel3.add(chatPanel,gc);
    	
    	inputFilePanel = new JPanel();
    	inputFilePanel.setOpaque(false);
    	inputFilePanel.setLayout(new GridBagLayout());

    	btnAddFile = new ImageButton("share1.png");
    	btnAddFile1a = new ImageIcon("share1a.png");
        btnAddFilePressed = new ImageIcon("share1b.png");
		btnAddFile.setRolloverIcon(btnAddFile1a);
		btnAddFile.setPressedIcon(btnAddFilePressed);
        btnAddFile.setBorder(null);
        btnAddFile.setContentAreaFilled(false);
        gc.gridx = 0;
		gc.gridy = 0;
        gc.insets = new Insets(2,60,0,50);
        btnAddFile.addActionListener(this);
        inputFilePanel.add(btnAddFile,gc);
        
        btnBack = new ImageButton("back1.png");
    	btnBack1a = new ImageIcon("back1a.png");
        btnBackPressed = new ImageIcon("back1b.png");
		btnBack.setRolloverIcon(btnBack1a);
		btnBack.setPressedIcon(btnBackPressed);
        btnBack.setBorder(null);
        btnBack.setContentAreaFilled(false);
        gc.gridx = 1;
		gc.gridy = 0;
        gc.insets = new Insets(2,60,0,50);
        btnBack.addActionListener(this);
        inputFilePanel.add(btnBack,gc);

        mainPanel = new JPanel();
        mainPanel.setOpaque(false);
        mainPanel.setLayout(new GridBagLayout());
        gc.gridx = 0;
    	gc.gridy = 0;
    	gc.insets = new Insets(80,0,0,0);
    	mainPanel.add(chattingPanel2,gc);
    	gc.gridx = 0;
    	gc.gridy = 1;
    	gc.insets = new Insets(-20,0,0,0);
    	mainPanel.add(chattingPanel3,gc);
    	gc.gridx = 0;
    	gc.gridy = 2;
    	gc.insets = new Insets(20,0,0,0);
    	mainPanel.add(inputFilePanel,gc);
    	
    	topPanel.add(mainPanel,gc);

        mainFrame.setUndecorated(true);
	 	mainFrame.setBackground(new Color(0,0,0,0));
	    mainFrame.setResizable(false);
	    mainFrame.add(topPanel);
		mainFrame.setLocation(6,9);
	    mainFrame.setSize(800,800);
	    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    mainFrame.setVisible(true);
	    
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	    
	    imageBackground2 = new ImageIcon("shareful.png");
    	imageBackgroundSize2 = new Dimension(imageBackground2.getIconWidth(),imageBackground2.getIconHeight());
    	mainFrame2.setPreferredSize(imageBackgroundSize2);

		fileSharingPanel = new JPanel(){
            public void paintComponent(Graphics g){
                Image img1 = new ImageIcon("shareful.png").getImage();
                Dimension size = new Dimension(img1.getWidth(null), img1.getHeight(null));
                setPreferredSize(size);
                setMinimumSize(size);
                setMaximumSize(size);
                setSize(size);
                setLayout(null);
                g.drawImage(img1, 0, 0, null);
            }
        };
        fileSharingPanel.setBounds(0, 0, imageBackground.getIconWidth(), imageBackground.getIconHeight());
    	
    	JPanel sharePanel = new JPanel(new GridBagLayout());
    	sharePanel.setPreferredSize(new Dimension(400,400));
    	sharePanel.setOpaque(false);

		sendAFile = new ImageButton("send1.png");
    	sendAFile1a = new ImageIcon("send1a.png");
        sendAFilePressed = new ImageIcon("send1b.png");
		sendAFile.setRolloverIcon(sendAFile1a);
		sendAFile.setPressedIcon(sendAFilePressed);
        sendAFile.setBorder(null);
        sendAFile.setContentAreaFilled(false);
    	gc2.gridx = 0;
    	gc2.gridy = 0;
    	gc2.insets = new Insets(160,-50,0,0);
    	sendAFile.addActionListener(this);
    	sharePanel.add(sendAFile,gc2);
    	
    	receiveAFile = new ImageButton("receive1.png");
    	receiveAFile1a = new ImageIcon("receive1a.png");
        receiveAFilePressed = new ImageIcon("receive1b.png");
		receiveAFile.setRolloverIcon(receiveAFile1a);
		receiveAFile.setPressedIcon(receiveAFilePressed);
        receiveAFile.setBorder(null);
        receiveAFile.setContentAreaFilled(false);
    	gc2.gridx = 0;
    	gc2.gridy = 1;
    	gc2.insets = new Insets(20,-50,0,0);
    	receiveAFile.addActionListener(this);
    	sharePanel.add(receiveAFile,gc2);
		
    	btnBackShare = new ImageButton("backShare1.png");
    	btnBackShare1a = new ImageIcon("backShare1a.png");
        btnBackSharePressed = new ImageIcon("backShare1b.png");
		btnBackShare.setRolloverIcon(btnBackShare1a);
		btnBackShare.setPressedIcon(btnBackSharePressed);
        btnBackShare.setBorder(null);
        btnBackShare.setContentAreaFilled(false);
    	gc2.gridx = 0;
    	gc2.gridy = 2;
    	gc2.insets = new Insets(10,-50,0,0);
    	btnBackShare.addActionListener(this);
    	sharePanel.add(btnBackShare,gc2);
    	
    	fileSharingPanel.add(sharePanel,gc2);
    	
    	mainFrame2.setUndecorated(true);
	 	mainFrame2.setBackground(new Color(0,0,0,0));
	    mainFrame2.setResizable(false);
	    mainFrame2.setVisible(false);
	    mainFrame2.add(fileSharingPanel);
		mainFrame2.setLocation(6,9);
	    mainFrame2.setSize(800,800);
	    mainFrame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

/////////////////////////////////////////////////////////////////////////////////////////
		imageBackground3 = new ImageIcon("third.png");
    	imageBackgroundSize3 = new Dimension(imageBackground3.getIconWidth(),imageBackground3.getIconHeight());
    	mainFrame3.setPreferredSize(imageBackgroundSize3);

		receivePanel = new JPanel(){
            public void paintComponent(Graphics g){
                Image img3 = new ImageIcon("third.png").getImage();
                Dimension size = new Dimension(img3.getWidth(null), img3.getHeight(null));
                setPreferredSize(size);
                setMinimumSize(size);
                setMaximumSize(size);
                setSize(size);
                setLayout(null);
                g.drawImage(img3, 0, 0, null);
            }
        };
        receivePanel.setBounds(0, 0, imageBackground3.getIconWidth(), imageBackground3.getIconHeight());
        
        receivePanel2 = new JPanel(new GridBagLayout());
    	receivePanel2.setPreferredSize(new Dimension(600,600));
   		receivePanel2.setOpaque(false);
    	
    	senderIP = new JTextField(35);
    	gc3.gridx = 0;
    	gc3.gridy = 0;
    	receivePanel2.add(senderIP,gc3);
    	
    	JPanel receivePanel3 = new JPanel(new GridBagLayout());
    	receivePanel3.setPreferredSize(new Dimension(400,400));
    	receivePanel3.setOpaque(false);
    	
    	okayReceive = new ImageButton("okay.png");
    	okayReceive1a = new ImageIcon("okay1a.png");
        okayReceivePressed = new ImageIcon("okay1b.png");
		okayReceive.setRolloverIcon(okayReceive1a);
		okayReceive.setPressedIcon(okayReceivePressed);
        okayReceive.setBorder(null);
        okayReceive.setContentAreaFilled(false);
    	gc3.gridx = 0;
    	gc3.gridy = 1;
    	gc3.insets = new Insets(2,10,0,5);
    	okayReceive.addActionListener(this);
    	receivePanel3.add(okayReceive,gc3);  	
    	
    	backReceive = new ImageButton("backReceive.png");
    	backReceive1a = new ImageIcon("backReceive1a.png");
        backReceivePressed = new ImageIcon("backReceive1b.png");
		backReceive.setRolloverIcon(backReceive1a);
		backReceive.setPressedIcon(backReceivePressed);
        backReceive.setBorder(null);
        backReceive.setContentAreaFilled(false);
    	gc3.gridx = 1;
    	gc3.gridy = 1;
   		gc3.insets = new Insets(2,10,0,5);
    	backReceive.addActionListener(this);
    	receivePanel3.add(backReceive,gc3);
    	
    	JPanel combined = new JPanel();
    	combined.setOpaque(false);
        combined.setLayout(new GridBagLayout());
     	gc3.gridx = 0;
    	gc3.gridy = 0;
    	gc3.insets = new Insets(-350,60,0,25);
    	combined.add(receivePanel2,gc3);
    	gc3.gridx = 0;
    	gc3.gridy = 1;
    	gc3.insets = new Insets(-200,60,0,25);
    	combined.add(receivePanel3,gc3);

    	receivePanel.add(combined,gc3);
    	
    	mainFrame3.setUndecorated(true);
	 	mainFrame3.setBackground(new Color(0,0,0,0));
	    mainFrame3.setResizable(false);
	    mainFrame3.setVisible(false);
	    mainFrame3.add(receivePanel);
		mainFrame3.setLocation(6,9);
	    mainFrame3.setSize(500,500);
	    mainFrame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    	
    }

    class ImageButton extends JButton {
  		public ImageButton(String img) {
    		this(new ImageIcon(img));
   		}

  		public ImageButton(ImageIcon icon) {
		    setIcon(icon);
		    setMargin(new Insets(0, 0, 0, 0));
		    setIconTextGap(0);
		    setBorderPainted(false);
		    setBorder(null);
		    setText(null);
		    setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
  		}

	}

    public void actionPerformed(ActionEvent event){
	  	if (event.getSource().equals(btnAddFile)){
	  		mainFrame2.setVisible(true);
	  		mainFrame.dispose();
	  	}else if(event.getSource().equals(btnBack)){
	  		mainFrame.dispose();
	  		System.exit(0);
	  	}else if(event.getSource().equals(sendAFile)){
	  		mainFrame2.setVisible(true);
	  	}else if(event.getSource().equals(receiveAFile)){
	  		mainFrame2.dispose();
	  		mainFrame3.setVisible(true);
	  	}else if(event.getSource().equals(btnBackShare)){
	  		mainFrame.setVisible(true);
	  		mainFrame2.dispose();
	  	}else if(event.getSource().equals(backReceive)){
	  		mainFrame2.setVisible(true);
	  		mainFrame3.dispose();
	  	}

	}

    private void getFileName(){
		fileChooser = new JFileChooser();
		int returnValue = fileChooser.showOpenDialog(null);
	  	if(returnValue == JFileChooser.CANCEL_OPTION) return;

	  	filename = fileChooser.getSelectedFile().getAbsolutePath();
        uploadFileTxtField.setText(filename);
    }

    public String getFile(){
    	return filename;

    }

    public String getServerAddress() {
        return JOptionPane.showInputDialog(
            null,
            "Enter IP Address of the Server:",
            "Welcome to the Chatter",
            JOptionPane.QUESTION_MESSAGE);
    }

    public String getUserName() {
        return JOptionPane.showInputDialog(
            null,
            "Choose a screen name:",
            "Screen name selection",
            JOptionPane.PLAIN_MESSAGE);
    }

    protected void run() throws IOException {
    	
        String serverAddress = getServerAddress();
        Socket socket = new Socket(serverAddress, 9001);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(socket.getOutputStream(), true);
		OutputStream output = socket.getOutputStream();
        while (true) {
            String line = in.readLine();
            if (line.startsWith("SUBMITNAME")) {
                out.println(getUserName());
            } else if (line.startsWith("NAMEACCEPTED")) {
                inputMessageField.setEditable(true);
            } else if (line.startsWith("MESSAGE")) {
                messageArea.append(line.substring(8) + "\n");
            }

            if (line.equals("READY\n")){
            	System.out.print("HEHEHE");
            	FileInputStream file = new FileInputStream(filename);
 				byte[] buffer = new byte[socket.getSendBufferSize()];
 				int bytesRead = 0;
 				while((bytesRead = file.read(buffer))>0){
                    output.write(buffer,0,bytesRead);
                }

            }
        }
    }
    public static void main(String args[])throws Exception{
     Client client = new Client();
     client.run();	
    }
    
}